package com.smarthome.smart;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import com.smarthome.smart.*;

public class MenuSmartAr extends Activity {
	TextView LabelMenu;
	String On_Of = "";
	GridView gridView;
	ArrayList<Item> gridArray = new ArrayList<Item>();
	CustomGridViewAdapter customGridAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.menu_grid_item);

		LabelMenu = (TextView) findViewById(R.id.lbMenu);
		LabelMenu.setText("Ar Condicionado");

		// set grid view item
		Bitmap homeIcon = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.casa);
		Bitmap userIcon = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.quarto);
		gridArray.add(new Item(homeIcon, "Ar Gree"));
		gridArray.add(new Item(userIcon, "Ar Consul"));
		gridView = (GridView) findViewById(R.id.gridView1);
		customGridAdapter = new CustomGridViewAdapter(this, R.layout.menu_grid,
				gridArray);
		gridView.setAdapter(customGridAdapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {

				if (gridArray.get(position).getTitle() == "Ar Gree") {
					Intent it = new Intent(getBaseContext(), MenuSmartControleRemotoGree.class);
					startActivity(it);	

				}

				if (gridArray.get(position).getTitle() == "Ar Consul") {
					Intent it = new Intent(getBaseContext(), MenuSmartControleRemotoConsul.class);
					startActivity(it);

				}

				Toast.makeText(getApplicationContext(),
						gridArray.get(position).getTitle() + " - " + On_Of,
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void enviarComando(String Tipo, String Canal) {
		String Retorno, NovoValor = "";
		SmartConfiguracaoInicial SmartConfig = new SmartConfiguracaoInicial();
		Retorno = SmartConfig.buscaDadosnoBanco(Tipo);

		if (Retorno.equals("0")) {
			NovoValor = "1";
			On_Of = "Ligado";

		} else {
			NovoValor = "0";
			On_Of = "Desligado";
		}
		enviarCamandoArduino(Canal, NovoValor,
				SmartConfig.buscaDadosnoBanco("IP"));
		SmartConfig.Smartupdate(NovoValor, Tipo);

	}

	public void enviarCamandoArduino(String Canal, String Valor, String IP) {

		ArrayList<NameValuePair> parametrosPost = new ArrayList<NameValuePair>();
		parametrosPost.add(new BasicNameValuePair(Canal, Valor));
		String RespostaRetornada, URLGET;
		URLGET = IP + "/" + Canal + "=" + Valor;

		try {
			// + "/LIVEMOBILEBICOALTERARPRECO.rule?sys=TEC",
			ConexaoHttpClient.executaHttpGet(URLGET);

		} catch (Exception erro) {

		}

	}

}
